(function() {
	
	Cube.Class = {
		$className: 'Cube.Class',
		
		extendingClass: {},

		/**
		 * Create class
		 * 
		 * If there is only one parameter, which is an object, it will be used as the class context.
		 * 
		 * @param {Function} 	parent		The object to inherit from
 		 * @param {Object} 		current		The current object
		 */
		create: function(parent, object) {			
			if (arguments.length == 1 && typeof parent == "object")
			{
				object = parent;
				parent = false;
			}

			// Make a global variable of the classname
			Cube.ClassManager.defineClassGlobal(object.$className);

			// Create the class constructor
			var evalStr = object.$className + ' = function() {' +
				'if (arguments[0] == Cube.Class.extendingClass) return;' +
				'this.constructor.apply(this, arguments);' +
			'};' +
			'var constuctor = ' + object.$className + ';';

			eval(evalStr);

			if (parent)
			{
				constuctor.prototype = new parent(Cube.Class.extendingClass);
			}

			// Give every method the parent method inside, so that they can call there super
			if (object)
			{
				Cube.Class.inherit(constuctor.prototype, object);
			}

			return constuctor;
		},

		/**
		 * Mixin interface
		 * 
		 * Mix an interface class into a existing Class.
		 * 
		 * @param {Object|Function} object
		 * @param {Object|Function} interface
		 * @param {Boolean} force If set to true, the interface properties will overwrite the other ones
		 * @return {Object} The original object with the interface mixed in
		 */
		mixin: function(object, interface, force) {
			var _object    = (typeof object == "function") ? object.prototype : object;
			var _interface = (typeof interface == "function") ? interface.prototype : interface;

			for (i in _interface)
			{
				if (typeof _object[i] !== 'undefined' && force !== true) continue;

				_object[i] = _interface[i];
			}

			return object;
		},

		/**
		 * Create a singleton from an constructor function
		 * 
		 * @param {Function} object
		 * @return {Object} 
		 */
		singleton: function(object) {
			var klass = new object();

			return klass;
		},

		/**
		 * Inherit members from parent object
		 * 
		 * This also creates the parent method, which can be used to call the super of the class. 
		 * 
 		 * @param {Object} dest
 		 * @param {Object} src
 		 * @param {Object} fname
		 */
		inherit : function(dest, src, fname) {
			if (arguments.length == 3) 
			{
				var ancestor 	= dest[fname];
				var descendent 	= src[fname];
				var method 		= descendent;

				descendent = function() {					
					var ref = this.parent;
					this.parent = ancestor;

					// Support for calling other methods from parent
					/*if (arguments.length > 0)
					{
						method = dest[arguments[0]];
						arguments = arguments[1] || [];
					}*/

					var result = method.apply(this, arguments);					
					ref ? this.parent = ref : delete this.parent;
					return result;
				};

				descendent.valueOf = function() {
					return method;
				};

				descendent.toString = function() {
					return method.toString();
				};

				dest[fname] = descendent;
			} 
			else 
			{
				for (var prop in src) 
				{
					if (typeof (src[prop]) == 'function') 
					{
						Cube.Class.inherit(dest, src, prop);
					} else 
					{
						dest[prop] = src[prop];
					}
				}
			}

			return dest;
		}

	};
	
})();
